package com.hongchen.qrcode;

import com.jfinal.kit.PathKit;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

/**
 * Created by 录康哲 on 2018/3/28.
 */
public class WaterMarjUtils {
    /**
     * @param srcImgPath 源图片路径
     * @param tarImgPath 保存的图片路径
     * @param waterMarkContent 水印内容
     * @param markContentColor 水印颜色
     * @param font 水印字体
     */
    public void addWaterMark(String srcImgPath, String tarImgPath, String waterMarkContent,String id,String time,String end_time,String str,Color markContentColor,Font font) {

        try {
            // 读取原图片信息
            File srcImgFile = new File(srcImgPath);//得到文件
             Image srcImg = ImageIO.read(srcImgFile);//文件转化为图片
            int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
            int srcImgHeight = srcImg.getHeight(null);//获取图片的高
            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            g.setColor(markContentColor); //根据图片的背景设置水印颜色
            g.setFont(font);              //设置字体
            //设置水印的坐标
            int x = 138;
            int y = 519;
            g.drawString(waterMarkContent, x, y);  //画出水印 - 名字
            g.drawString(id, 481, 521);  //画出水印 - 电话
            g.drawString(time, 191, 576);  //画出水印 - 家庭成员
            g.drawString(end_time, 142, 630);  //画出水印 - 结束日期
//            g.drawString(str,142,643);//需求
            String[]  strs=str.split("-");
            int yy=643;
            for(int i=0,len=strs.length;i<len;i++){
                System.out.println(strs[i].toString());
                g.drawString(strs[i].toString(),142,yy);//需求
                yy=yy+20;
            }

            g.dispose();
            // 输出图片
            FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
            ImageIO.write(bufImg, "jpg", outImgStream);
            System.out.println("添加水印完成");
            outImgStream.flush();
            outImgStream.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public int getWatermarkLength(String waterMarkContent, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }
    public static void main(String[] args) {
        Font font = new Font("微软雅黑", Font.PLAIN, 25);//水印字体
        System.out.println(PathKit.getWebRootPath()+"\\src\\main\\webapp\\msg\\report.jpg");
        String srcImgPath= PathKit.getWebRootPath()+"\\src\\main\\webapp\\msg\\report3.png"; //源图片地址
        String tarImgPath= PathKit.getWebRootPath()+"\\src\\main\\webapp\\msg\\report_alter3.png"; //待存储的地址
        String waterMarkContent="阿娇姣";  //水印内容
        String id="13073746893";
        String time="8人";
        String end_time="1";
        String str="嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿-嘿嘿嘿嘿嘿嘿嘿嘿嘿";
        String sb = "bbbdsajjds";

        System.out.println(sb.substring(0, 4));
        Color color=new Color(0,0,0,255);                               //水印图片色彩以及透明度
        System.out.println(end_time.length());
        new WaterMarjUtils().addWaterMark(srcImgPath, tarImgPath, waterMarkContent,id,time,end_time,str,color,font);

    }
}

